package com.lancoo.klgcourseware.ui.fragment.operate;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.WordArticulation;
import com.lancoo.klgcourseware.entity.bean.SpellCharacterBean;
import com.lancoo.klgcourseware.ui.adapter.SpellMemoryCharacterAdapter;
import com.lancoo.klgcourseware.ui.fragment.basicTrain.BasicTrainBaseFragment;
import com.lancoo.klgcourseware.utils.KlgToolUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pengmi on 2019/8/28 17:11
 * Description:全拼默写引导图
 */
public class AlertSpellMemoryWriteTrainOperateFragment extends AlertBaseFragment {
    protected List<SpellCharacterBean> spellCharacterBeans;

    @Override
    protected int getContentViewId() {
        return R.layout.klg_fragment_alert_spell_memory_write_train;
    }

    @Override
    protected void initView(View view) {
        String klgName=getArguments().getString("klgName");
        splitSpellData();
        RecyclerView recyclerView = view.findViewById(R.id.alert_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
        SpellMemoryCharacterAdapter mAdapter = new SpellMemoryCharacterAdapter(getContext(),spellCharacterBeans);
        mAdapter.setTrainModel(true,false);
        mAdapter.setAlert(true);
        recyclerView.setAdapter(mAdapter);
        view.findViewById(R.id.tv_i_know).setOnClickListener(this);

        RelativeLayout rl_spell = view.findViewById(R.id.rl_spell_content_alert);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) rl_spell.getLayoutParams();
        int topMargin=KlgToolUtils.dip2px(getContext(),KlgToolUtils.isPad(getContext())?210:202);
        layoutParams.topMargin= KlgToolUtils.getStatusHeight(getContext())+topMargin;
        rl_spell.setLayoutParams(layoutParams);
        if(getArguments().getBoolean("isOnlyShowSpellAlert")){
            view.findViewById(R.id.view_circle).setVisibility(View.INVISIBLE);
            view.findViewById(R.id.view_circle1).setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 单词拆分，作答总时长统计
     */
    private void splitSpellData(){
        spellCharacterBeans=new ArrayList<>();
        StringBuilder stringBuilder=new StringBuilder();
        WordArticulation wordArticulation = (WordArticulation) getArguments().getSerializable(BasicTrainBaseFragment.KEY_SPELL_ARTICULATION);
        String syllable = wordArticulation.getSyllable();
        int index=1;
        boolean haiPoint=false;
        boolean haiLine=false;
        //拼写,快速环节数据组装
        for (int i = 0; i < syllable.length(); i++) {

            if(TextUtils.equals("·",syllable.charAt(i)+"")){
                haiPoint=true;
                continue;
            }
            if(TextUtils.equals("-",syllable.charAt(i)+"")){
                haiLine=true;
                continue;
            }
            SpellCharacterBean spellCharacterBean=new SpellCharacterBean();
            spellCharacterBean.setCharacter(syllable.charAt(i)+"");
            spellCharacterBean.setHasPoint(haiPoint);
            spellCharacterBean.setHasLine(haiLine);
            spellCharacterBeans.add(spellCharacterBean);
            stringBuilder.append(syllable.charAt(i)+" ");
            index++;
            haiPoint=false;
            haiLine=false;
        }
        stringBuilder.append(","+wordArticulation.getKlgName());
    }

    @Override
    public void onClick(View v) {
        if(System.currentTimeMillis()-time<500){
            return;
        }
        time=System.currentTimeMillis();
        super.onClick(v);
        int id=v.getId();
        if(id==R.id.tv_i_know){
            closeAlertOperate();
        }
    }
}
